package com.quectel.sys.module.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.sys.constants.RedisCacheConstants;
import com.quectel.sys.module.sys.dao.SysGroupDao;
import com.quectel.sys.module.sys.dto.SysGroupDto;
import com.quectel.sys.module.sys.dto.SysUserDto;
import com.quectel.sys.module.sys.entity.SysGroupEntity;
import com.quectel.sys.module.sys.service.SysGroupService;
import com.quectel.sys.module.sys.service.SysUserService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-05-31 11:40:38
 */
@DubboService
public class SysGroupServiceImpl implements SysGroupService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysGroupServiceImpl.class);

    @Autowired
    private SysGroupDao sysGroupDao;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysGroupService sysGroupService;


    @Override
    public SysGroupDto selectByCode(String code, Long tenantId) {
        SysGroupEntity sysGroupEntity = sysGroupDao.selectOne(new LambdaQueryWrapper<SysGroupEntity>()
                .eq(SysGroupEntity::getCode, code)
                .eq(SysGroupEntity::getTenantId, tenantId)
                .last(SqlConstants.MYSQL_LIMIT_ONE));
        return CopyUtils.copyObj(sysGroupEntity, SysGroupDto.class);
    }

    @Override
    public List<SysUserDto> getSubUsers(Long groupId) {
        List<SysUserDto> result = new ArrayList<>();
        List<SysGroupDto> sysGroupDtos = selectByParentId(groupId);
        sysGroupDtos.add(selectById(groupId));
        for (SysGroupDto sysGroupDto : sysGroupDtos) {
            List<SysUserDto> list = sysUserService.selectListByGroupId(sysGroupDto.getId());
            if (list != null && list.size() > 0) {
                result.addAll(list);
            }

        }
        result.forEach(v -> {
            //填充信息
            for (SysGroupDto sysGroupDto : sysGroupDtos) {
                if (Objects.equals(sysGroupDto.getId(), v.getGroupId())) {
                    v.setSysGroup(sysGroupDto);
                }
            }
        });
        return result;
    }

    @Override
    public List<SysGroupDto> selectByParentId(Long parentId) {
        List<SysGroupDto> list = new ArrayList<>();

        List<SysGroupEntity> entityList = sysGroupDao.selectList(new QueryWrapper<SysGroupEntity>().eq("parent_id", parentId).orderByAsc("sort"));
        list.addAll(CopyUtils.copyList(entityList, SysGroupDto.class));

        if (entityList != null && !entityList.isEmpty()) {
            for (SysGroupEntity sysGroupEntity : entityList) {
                list.addAll(selectByParentId(sysGroupEntity.getId()));
            }
        }
        return list;
    }

    @Override
    public List<SysGroupDto> queryList(Map<String, Object> params) {
        List<SysGroupEntity> list = sysGroupDao.queryList(params);
        return CopyUtils.copyList(list, SysGroupDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return sysGroupDao.queryTotal(params);
    }

    @Override
    public Long save(SysGroupDto dto) {
        SysGroupEntity entity = CopyUtils.copyObj(dto, SysGroupEntity.class);
        entity.setId(Snowflake.nextId());
        sysGroupDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SysGroupDto dto) {
        SysGroupEntity entity = CopyUtils.copyObj(dto, SysGroupEntity.class);
        sysGroupDao.updateById(entity);
    }

    @Override
    public SysGroupDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                RedisCacheConstants.SYS_GROUP_BY_ID + id,
                5 * 60
        );
    }

    @Override
    public SysGroupDto selectById(Long id) {
        SysGroupEntity entity = sysGroupDao.selectById(id);
        return CopyUtils.copyObj(entity, SysGroupDto.class);
    }

    @Override
    public void deleteById(Long id) {
        sysGroupDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }

}
